package cn.hao.redisspringbot;

import cn.hao.redisspringbot.utils.RedisUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
import java.util.*;

public class test {
    //redis工具类，依赖自带的
   @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public  void contextLoads() {
        Set<String> keys =stringRedisTemplate.keys("*");
        keys.forEach(item-> System.out.println(item));
    }

   @Test
    public void test1() {
        //  stringRedisTemplate.expire("name",300, TimeUnit.SECONDS);
        Long name = stringRedisTemplate.getExpire("name");
        System.out.println(name);
    }

    public void  test4(){
        stringRedisTemplate.opsForList(). leftPush("names","张三");
        stringRedisTemplate.opsForList().leftPushAll("names", "张三","李四","王五");
        List<String> list=new ArrayList<String>();
        list.add(0,"老大");
        list.add(1,"老二");
        list.add(2,"老三");
        stringRedisTemplate.opsForList(). leftPushAll("names", list);
        List<String> names = stringRedisTemplate.opsForList() .range("names",0,-1);
        names.forEach(System.out::println);
    }

    @Resource
    RedisUtil redisUtil;


    public void Test3(){
        redisUtil.hset("user2","username","nx");
        redisUtil.hset("user2","password","123456");
        System.out.println(redisUtil.hmget("user2"));
        Map<String,Object> map=new HashMap<>();
        map.put("name","nx");
        map.put("password","123456");
        map.put("age",18);
        redisUtil.hmset("user3",map);
        System.out.println(redisUtil.hmget("user3"));

    }

    public void test5() {

//        User user=new User();

//        user.setUserName("admin");

//        user.setPassword("123456");

//        user.setSex("男");

//        user.setAge("35");

//        user.setRedoid("2");

//        Map<String,Object> map=new HashMap<String,Object>();

//        map.put("userName",user.getUserName());

//        map.put("passwordd",user.getPassword());

//        map.put("sex",user.getSex());

//        map.put("age",user.getAge());

//        redisUtil.hmset("user1",map);

//        System.out.println(redisUtil.hmget("user1"));

//        Map<Object,Object> gmap=redisUtil.hmget("user1");

//        //遍历

//        for(Map.Entry<String,Object> item:map.entrySet()){

//            System.out.println(item.getKey()+":"+item.getValue());

//        }


    }
}
